package youcaihuaoa.weetop.com.ui.apply

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CustomListItemBean
import com.robot.base.util.arouter.ARouterUtil
import youcaihuaoa.weetop.com.BR
import youcaihuaoa.weetop.com.R
import youcaihuaoa.weetop.com.databinding.AppActvityMyApplyBinding
import youcaihuaoa.weetop.com.ui.adapter.MyApplyAdapter
import youcaihuaoa.weetop.com.vm.apply.MyApplyViewModel

@Route(path = AppConstants.Router.Main.A_MY_APPLY)
class MyApplyActivity : GLBaseActivity<AppActvityMyApplyBinding, MyApplyViewModel>() {

    private lateinit var kqAdapter: MyApplyAdapter

    private lateinit var rsAdapter: MyApplyAdapter

    private lateinit var xzAdapter: MyApplyAdapter

    private lateinit var cwAdapter: MyApplyAdapter

    override fun initComponents() {
        addSingleTitleBar("我的申请")
        kqAdapter = MyApplyAdapter().apply {
            setOnItemClickListener { _, _, position ->
                val id = kqAdapter.data[position].id
                if (TextUtils.isEmpty(id)) {
                    //固定模版
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle()
                            .apply
                            {
                                putString(AppConstants.BundleKey.ID, id)
                                putString(
                                    AppConstants.BundleKey.TYPE,
                                    kqAdapter.data[position].type
                                )
                                putString(
                                    AppConstants.BundleKey.TITLE,
                                    kqAdapter.data[position].title
                                )
                            })
                    return@setOnItemClickListener
                }
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                    Bundle()
                        .apply
                        {
                            putString(AppConstants.BundleKey.ZDY_FORM_ID, id)
                            putString(AppConstants.BundleKey.TYPE, kqAdapter.data[position].type)
                            putString(AppConstants.BundleKey.TITLE, kqAdapter.data[position].title)
                        })
            }
        }
        rsAdapter = MyApplyAdapter().apply {
            setOnItemClickListener { _, _, position ->
                val id = rsAdapter.data[position].id
                if (TextUtils.isEmpty(id)) {
                    //固定模版
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle()
                            .apply
                            {
                                putString(AppConstants.BundleKey.ID, id)
                                putString(
                                    AppConstants.BundleKey.TYPE,
                                    rsAdapter.data[position].type
                                )
                                putString(
                                    AppConstants.BundleKey.TITLE,
                                    rsAdapter.data[position].title
                                )
                            })
                    return@setOnItemClickListener
                }
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                    Bundle()
                        .apply
                        {
                            putString(AppConstants.BundleKey.ZDY_FORM_ID, id)
                            putString(AppConstants.BundleKey.TYPE, rsAdapter.data[position].type)
                            putString(AppConstants.BundleKey.TITLE, rsAdapter.data[position].title)
                        })
            }
        }
        xzAdapter = MyApplyAdapter().apply {
            setOnItemClickListener { _, _, position ->
                val id = xzAdapter.data[position].id
                if (TextUtils.isEmpty(id)) {
                    //固定模版
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle()
                            .apply
                            {
                                putString(AppConstants.BundleKey.ID, id)
                                putString(
                                    AppConstants.BundleKey.TYPE,
                                    xzAdapter.data[position].type
                                )
                                putString(
                                    AppConstants.BundleKey.TITLE,
                                    xzAdapter.data[position].title
                                )
                            })
                    return@setOnItemClickListener
                }
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                    Bundle()
                        .apply
                        {
                            putString(AppConstants.BundleKey.ZDY_FORM_ID, id)
                            putString(AppConstants.BundleKey.TYPE, xzAdapter.data[position].type)
                            putString(AppConstants.BundleKey.TITLE, xzAdapter.data[position].title)
                        })
            }
        }
        cwAdapter = MyApplyAdapter().apply {
            setOnItemClickListener { _, _, position ->
                val id = cwAdapter.data[position].id
                if (TextUtils.isEmpty(id)) {
                    //固定模版
                    ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_MY_APPLY_PROFILE,
                        Bundle()
                            .apply
                            {
                                putString(AppConstants.BundleKey.ID, id)
                                putString(
                                    AppConstants.BundleKey.TYPE,
                                    cwAdapter.data[position].type
                                )
                                putString(
                                    AppConstants.BundleKey.TITLE,
                                    cwAdapter.data[position].title
                                )
                            })
                    return@setOnItemClickListener
                }
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_TREND_APPLY_PROFILE,
                    Bundle()
                        .apply
                        {
                            putString(AppConstants.BundleKey.ZDY_FORM_ID, id)
                            putString(AppConstants.BundleKey.TYPE, cwAdapter.data[position].type)
                            putString(AppConstants.BundleKey.TITLE, cwAdapter.data[position].title)
                        })
            }
        }
        binding.recyclerKq.apply {
            layoutManager = GridLayoutManager(this@MyApplyActivity, 4)
            adapter = kqAdapter
        }
        binding.recyclerRs.apply {
            layoutManager = GridLayoutManager(this@MyApplyActivity, 4)
            adapter = rsAdapter
        }
        binding.recyclerXz.apply {
            layoutManager = GridLayoutManager(this@MyApplyActivity, 4)
            adapter = xzAdapter
        }
        binding.recyclerCw.apply {
            layoutManager = GridLayoutManager(this@MyApplyActivity, 4)
            adapter = cwAdapter
        }
    }

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_actvity_my_apply

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewObservable() {
        viewModel.uiEvent.menuDataLiveEvent.observe(this) {
            val kqList = it.kq_form_data
            val rsList = it.rs_form_data
            val xzList = it.xz_form_data
            val cwList = it.cw_form_data
            refreshKQ(kqList)
            refreshRS(rsList)
            refreshXZ(xzList)
            refreshCW(cwList)
        }
    }

    override fun initData() {

        viewModel.refreshMenuData()

    }

    private fun refreshKQ(list: MutableList<CustomListItemBean>) {
        list.forEach {
            it.icon = R.mipmap.ic_menu_kq
        }
        list.addAll(0, viewModel.kqLockData)
        kqAdapter.setNewInstance(list)
    }

    private fun refreshRS(list: MutableList<CustomListItemBean>) {
        list.forEach {
            it.icon = R.mipmap.ic_menu_rs
        }
        list.addAll(0, viewModel.rsLockData)
        rsAdapter.setNewInstance(list)
    }

    private fun refreshXZ(list: MutableList<CustomListItemBean>) {
        list.forEach {
            it.icon = R.mipmap.ic_menu_xz
        }
        list.addAll(0, viewModel.xzLockData)
        xzAdapter.setNewInstance(list)
    }

    private fun refreshCW(list: MutableList<CustomListItemBean>) {
        list.forEach {
            it.icon = R.mipmap.ic_menu_cw
        }
        list.addAll(0, viewModel.cwLockData)
        cwAdapter.setNewInstance(list)
    }


}